package com.kexin.jbsg.util;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

import android.app.Activity;
import android.content.Context;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Build;
import android.telephony.TelephonyManager;
import android.text.format.Formatter;
import android.util.Log;

/**
 * @author oven:
 * @version 创建时间：2013-2-28 上午10:49:31
 * 
 */
public class PhoneInfoUtil {
	
	/**
	 * 获取系统数据
	 * 型号,内存,系统版本,mac
	 * @param activity
	 * @return
	 */
	public static String getPhoneInfoString(Activity activity){
		StringBuilder result = new StringBuilder();
		result.append(getPhoneName(activity));
		result.append(","+getSystemMem(activity));
		result.append(","+getSystemVersion());
		result.append(","+getMacAddress(activity));
		return result.toString();
	}
	
	/**
	 * 获取手机信息并返回到Unity
	 * @param activity
	 */
	public static String getPhoneInfo(Activity activity){
    	String info = getPhoneInfoString(activity);
    	return info;
    }
	
	/**
	 * 需要权限
	 * <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
	 * @param activity
	 * @return
	 */
	public static String getPhoneName(Activity activity){
		TelephonyManager mTm = (TelephonyManager) activity.getSystemService(Activity.TELEPHONY_SERVICE);
		String imei = mTm.getDeviceId();  
	    String imsi = mTm.getSubscriberId();
		String mtype = android.os.Build.MODEL; // 手机型号  
	    String numer = mTm.getLine1Number(); // 手机号码，有的可得，有的不可得  
	    Log.i("infos","手机型号:"+mtype);
	    return mtype;
	}
	
	public static String getSystemVersion(){
		String result = Build.VERSION.RELEASE;
		Log.i("infos","系统版本:"+Build.VERSION.SDK_INT+" "+Build.VERSION.RELEASE);
		return result;
	}
	
	/**
	 * 需要权限
	 * <uses-permission android:name="android.permission.READ_PHONE_STATE" />
	 * @param activity
	 * @return
	 */
	public static String getMacAddress(Activity activity){
		WifiManager wifiManager = (WifiManager) activity.getSystemService(Context.WIFI_SERVICE);  
	    WifiInfo wifiInfo = wifiManager.getConnectionInfo();  
		String result = wifiInfo.getMacAddress();
		Log.i("infos", "mac地址:" + result);
		return result;
	}
	
		  
    public static String getSystemMem(Activity activity){  
		String str1 = "/proc/meminfo";// 系统内存信息文件  
		String str2;        
		String[] arrayOfString;  
		long initial_memory = 0;  
		try{  
			FileReader localFileReader = new FileReader(str1);  
			BufferedReader localBufferedReader = new BufferedReader(    localFileReader, 8192);  
			str2 = localBufferedReader.readLine();// 读取meminfo第一行，系统总内存大小  
			arrayOfString = str2.split("\\s+");  
			initial_memory = Integer.valueOf(arrayOfString[1]).intValue() * 1024;// 获得系统总内存，单位是KB，乘以1024转换为Byte       
			localBufferedReader.close();  
		}catch(IOException e){   
		}  
		String result = Formatter.formatFileSize(activity.getApplicationContext(), initial_memory);// Byte转换为KB或者MB，内存大小规格化
		Log.i("infos", "手机内存:" + result);
		return  result;
    }
}

